//
//  CNUILoadingLabel.swift
//  Voice
//
//  Created by 卢逢卫 on 2024/2/19.
//

import UIKit

public class CNUILoadingLabel: UILabel {
   

    public var clickBlock: (() -> ())?
    public var stop: Bool = false {
            didSet {
                if stop {
                    stopLoading()
                }
            }
        }
    var textname = ""
    var backView:UIView!
    var cicleView:UIView!
    
    public init(background: UIColor = .white, text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment = .left, numberOfLines: Int = 1, lineSpacing: CGFloat? = nil, strokeColor: UIColor? = UIColor.white) {
        super.init(frame: .zero)
        
        self.text = text
        self.textname = text
        self.textColor = textColor
        self.font = font
        self.textAlignment = alignment
        self.numberOfLines = numberOfLines
        self.backgroundColor = background
        
        if let lineSpacing = lineSpacing {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = lineSpacing
            let attributedString = NSAttributedString(string: text, attributes: [.paragraphStyle: paragraphStyle])
            self.attributedText = attributedString
        }
        
        if let strokeColor = strokeColor {
            let strokeTextAttributes: [NSAttributedString.Key: Any] = [
                .strokeColor: strokeColor,
                .foregroundColor: textColor,
                .strokeWidth: -2.0,
                .font: font
            ]
            self.attributedText = NSAttributedString(string: text, attributes: strokeTextAttributes)
        }
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        self.isUserInteractionEnabled = true
        self.addGestureRecognizer(tapGesture)
        
        self.backView = UIView()
        self.backView.backgroundColor = .white
        self.addSubview(self.backView)
        
        var point = UIView()
        point.frame = CGRect(x: 10, y: 5, width: 5, height: 5)
        point.layer.cornerRadius = 2.5
        point.backgroundColor = .white
        point.layer.masksToBounds = true
        cicleView = UIView()
        cicleView.backgroundColor = background
        cicleView.layer.cornerRadius = 20
        cicleView.layer.masksToBounds = true
        cicleView.addSubview(point)
        self.backView.addSubview(cicleView)
        self.backView.isHidden = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    @objc private func handleTap() {
//        if self.stop == false {
            startLoading()
        if self.clickBlock != nil {
            self.clickBlock!()
        }
//        }
        
        
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        self.backView.frame = CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height)
        cicleView.frame = CGRect(x: (bounds.width - 40)/2 , y: (bounds.height - 40)/2, width: 40, height: 40)
    }
    
    private func startLoading() {
        stop = true

//        self.isHidden = true
        self.isUserInteractionEnabled = false
        self.backView.isHidden = false
        
        let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
        rotationAnimation.toValue = NSNumber(value: Double.pi * 2)
        rotationAnimation.duration = 0.5
        rotationAnimation.repeatCount = .infinity
        rotationAnimation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
        self.cicleView.layer.add(rotationAnimation, forKey: "rotationAnimation")
    }
    
    private func stopLoading() {
        stop = false
        self.isUserInteractionEnabled = true
        self.text = self.textname
        self.backView.isHidden = true
        self.isUserInteractionEnabled = true
        self.cicleView.layer.removeAnimation(forKey: "rotationAnimation")
    }
}
